<?php

namespace app\job;

use think\facade\Log;
use think\queue\Job;

class Test
{
    public function fire(Job $job, $data)
    {
        echo 'test start' . PHP_EOL;
        // 处理任务的逻辑
        if ($this->doJob($data)) {
            echo 'job completed' . PHP_EOL;
            $job->delete();
        } else {
            Log::info('job failed' . json_encode($data, 320) . PHP_EOL);
            echo 'job failed' . PHP_EOL;
            if ($job->attempts() > 10) {
                echo 'job attempts exceeded' . PHP_EOL;
                $job->delete();
            }
            $job->release(5);
        }
        echo 'test end' . PHP_EOL;
    }

    /**
     * 业务处理
     * @param $data
     * @return bool
     */
    public function doJob($data)
    {
        // TODO 处理业务逻辑
        $now = time();
        $date = $data['date'];
        // TODO 比对时间 如果时间相差较短执行 如果超时 移除
        if ($now - strtotime($date) < 0) {
            return true;
        } else if ($now - strtotime($date) > 180) {
            return false;
        } else {
            //公海领取客户
            return true;
        }
        return true;
    }
}